﻿/*
页面通用部分 模板 <br/>
前置条件: jquery.js, underscore.js <br/>
@author gonglong fsl 20151004
 */
var commTemplUtil = {};

commTemplUtil.type = {
    /* 1.顶部, 登录, 注册 */
    login: "<div class='head com-w'><div class='wrapper clearfix'> " +
        "<div class='item fl'><%=welcome_txt %></div>" +
        "<div class='fr'><a href='<%=login_href %>' class='item f-red'>[登录]</a>" +
        "<a href='<%=register_href %>' class='item'>[免费注册]</a></div></div></div>",
    /* 1.2 顶部, 登录成功 */
    loginDone: "<div class='head com-w'><div class='wrapper clearfix'>" +
        "<div class='item fl'><%=welcome_txt %></div>" +
        "<div class='fr'>" +
        "<span class='item'><%=username %>, 您好！</span>" +
        "<a href='<%=cart_href %>' class='item f-red'>[我的购物车]</a>" +
        "<a href='<%=order_href %>' class='item f-red'>[我的订单]</a>" +
        "<a href='<%=buyer_center_href %>' class='item f-red'> <%if(user_type == '1'){%> [采购商中心] <% } else {%> [供应商中心] <%}%></a>" +
        "<a href='<%=logout_href %>' class='item'>退出登录</a></div></div></div>",

    /* 2.网站logo, 搜索框,二维码 */
    search: "<div class='top-logo-line com-w'><div class='wrapper clearfix'>" +
        "<form id='search_form' action='/search' method='post' class='clearfix'>" +
        "<a href='<%=home_url%>' class='logo'><img src='<%=web_logo_img%>' class='wh100' style='width: 184px;height: 125px;margin-top: 6px;margin-bottom: 6px;'></a>" +
        "<div class='search'><div class='search-type'>" +
        "<span class='item f-coffee' id='1'>供应</span><input name='id' type='hidden' value='1' id='roleid'><span>|</span>" +
        "<span class='item' id='2' >求购</span><span>|</span><span class='item' id='3'>商家</span></div>" +
        "<div class='search-box clearfix'><input type='text' id='search_text' class='text' placeholder='请输入搜索关键字' name='input_text'>" +
        "<input type='submit' class='button' value='搜索' ><input type='hidden' name='_token' value='<%= csrfToken %>'></div>" +
        "<div class='hot-key'><span class='tip-txt'>热门关键字:</span>" +
        "<% _.each(hot_words, function(worditem) { %>" +
        " <a href='<%=worditem.href_val%>' > <%=worditem.word_txt%> </a> " +
        "<% }); %>" +
        " </div></div>" +
        "<div class='d-bar-code fr'>" +
        "<div class='pic'><img src='<%=d_code_img%> ' class='wh100'></div>" +
        "<p class='txt'>APP下载</p>" +
        " </div></form></div></div>",
    /* 3. 头部 导航 */
    headNav: "<div id='head-comm-nav' class='com-nav com-w'><div class='wrapper clearfix'>" +
        "<ul class='clearfix'> " +
        " <% _.each(obj, function(navitem){ %> " +
        " <li><a href='<%=navitem.href_val %>' class=''><%=navitem.href_txt %></a> " +
        " <% if(navitem.new_flag){ %> <i class='new-item'></i> <% } %> </li>" +
        "<% });  %> " +
        "</ul></div></div>",
    /* 4. 底部导航 */
    footNav: "<div class='com-w mt20'><div class='wrapper clearfix'>" +
        "<div class='com-info-box w1200-box h150-box f12 bg-F9F9F9'>" +
        "<% _.each(obj, function(itemOut){ %><dl class='foot-info-list-item'>" +
        " <dt> <%=itemOut.item_desc%> </dt><dd>" +
        "<% _.each(itemOut.item_list ,function(itemin){ %>" +
        " <div><a href='<%=itemin.href_val%>'><%=itemin.href_txt%></a></div>" +
        "<% }); %></dd></dl>" +
        "<% }); %></div></div></div>",
    /*6.最底部导航*/
    bottomMenu: "<div class='com-w mt20 mb20' style='position:relative'><div class='wrapper clearfix'>" +
        "<div class='com-info-box w1200 h160 border-none tc'><div>" +
        "<ul class='list-line auto clearfix parsentsA' style='width:355px;'>" +
        "<% _.each(obj, function(itemOuts){ %>" +
        "<li class='after'><a href='<%=itemOuts.href_src%>' style='color:#666;margin-right:10px'><%=itemOuts.href_text%></a></li>" +
        "<% }); %>" +
        "</ul></div>" +
        "<p class='lh20 c-666'>建议使用IE9.0以上版本, 1366 X 768 及以上分辨率</p>" +
        "<p class='lh20 c-666'>备案号 : 1234567890000</p>" +
        "<p class='lh20 c-666'>" +
        "Copyright &copy; 2015 咖啡商谷. All rights reserved.</p>" +
        "</div></div>" +
        "<a class='back-top fr' id='to_top'></a>" +
        "</div>",
    /* 5.左侧菜单 */
    leftMenu: "<% _.each(obj , function(subMenuItem){  %>" +
        "<dl class='sub-menu-item'><dt><i class='i-sub-menu'></i>" +
        "<span class='lbl-txt'> <%=subMenuItem.title%> </span> </dt>" +
        "<% _.each(subMenuItem.menuItems, function(menuItem){ %>" +
        "<dd><a href='<%=menuItem.href_val %>' class='link-txt'>" +
        "<span class='lbl-txt'><%=menuItem.lbl_txt %></span>" +
        "<i class='icon-caret-right'></i></a></dd>" +
        "<% }); %>" +
        "</dl>" +
        "<% }); %> <div class='menu-bottom'></div>",
    /* C2 自由集散区 类别 */
    goodsCateg: "<div class='top-main-btn'><div id='tab-head-box' class='tab-head tab-btn clearfix'>" +
        "<% _.each(obj, function(item){ %>" +
        "<span  class='text' value='<%=item.value%>' id='<%=item.value%>'><%=item.title%></span>" +
        "<% }); %></div>" +
        "<div id='tab-detail-box' class='tab-main'>" +
        "<% _.each(obj, function(item){ %>" +
        "<ul class='clearfix' style='display:none;'>" +
        "<% _.each(item.detail, function(detaiItem){ %>" +
        "<li value='<%=detaiItem.value%>' id='<%=detaiItem.value%>'><a href='javascript:void(0)' class='text'><%=detaiItem.text%></a>" +
        "</li><% }); %></ul><% }); %></div></div>",
    errorDialog: "<div class='dialog-div'><div style='font-size: 15px;'><%= message %></div>" +
        "<div class='price-game-btn'><div class='btn btn-warn btn-w140x40' id='ok-btn' code='' style='margin: 0 auto; display:block;'>" +
        "<%= ok_txt %></div></div></div>",
    confirmDialog : "<div class='dialog-div'><div><%=message %></div>"+
    "<div class='price-game-btn'><div class='btn btn-gray btn-w140x40 cancel-btn'><%=cancel_txt %></div>" +
    "<div class='btn btn-warn btn-w140x40 ok-btn' code=''><%=ok_txt %></div>" +
    "</div></div>"

};

commTemplUtil.setTempl = function(domId, data, typeVal) {
    var templStr = commTemplUtil.type[typeVal];
    var templObj = _.template(templStr);
    var temphtml = templObj(data);
    $("#" + domId).replaceWith(temphtml);
};

commTemplUtil.addTempl = function(domId, data, typeVal) {
    var templStr = commTemplUtil.type[typeVal];
    var templObj = _.template(templStr);
    var temphtml = templObj(data);
    $("#" + domId).html(temphtml);
};

commTemplUtil.setDataByTempl = function(targetObjId, tmplId, data) {
    var tmplStr = _.template(("#" + tmplId).html());
    var tmplObj = _.template(tmplStr);
    var tmplHtml = tmplObj(data);
    $("#" + targetObjId).html(tmplHtml);
};


commTemplUtil.initPage = function() {

    if (COMM_DATA.topObj.loginFlag == '0') {
        commTemplUtil.setTempl("top_test", COMM_DATA.topObj, "login");
    } else {
        commTemplUtil.setTempl("top_test", COMM_DATA.topObj, "loginDone");
    }

    commTemplUtil.setTempl("web_logo_search", COMM_DATA.searchObj, "search");
    //首页 供应|求购|商家的切换
   
    $(".search-type > .item").click(function(){
        $(".search-type > .item").attr("class","item");
        $(this).attr("class","item f-coffee");
         $("#roleid").attr("value","");
        $("#roleid").attr("value",""+$(this).attr("id")+"")
       clickSearch();

    });
    //点击搜索 的事件

function clickSearch(){
    $(".button").click(function(){
    var getCodeVal =  $(".f-coffee").attr("code");
    //获取搜索 输入的文本框 的值
    var getText = $("#search_text").val();
        
        if (getText == null) {
            alert("请输入关键字");
        }
    });
}
    commTemplUtil.setTempl("comm_nav", COMM_DATA.mainNavObj, "headNav");
    commTemplUtil.addTempl("bottom_main", COMM_DATA.bottomObj, "bottomMenu");
    commTemplUtil.addTempl("leftmenuitems", COMM_DATA.leftNavObj, "leftMenu");
    commTemplUtil.setTempl("foot_nav", COMM_DATA.footNavObj, "footNav");

    commTemplUtil.setHeadCommNav(1);
};


/*
 翻页插件。
 @param domSelector : jq 选择器;
 @param pageConfig : 配置参数对象，
 pageConfig = {
     perpage : "每页记录数",
     page : "当前页码",
     total : "总记录数",
     onSelect : 回调函数,翻页事件.
 }
*/
commTemplUtil.initPaging = function(domSelector, pageConfig) {
    var conf = {
        pageSize: 0,
        page: 0,
        total: 0,
        onClick: function(page) {

        }
    };
    conf = $.extend({}, conf, pageConfig);

    var pagingObj = $(domSelector || ".comm-paging").empty();

    var paging = pagingObj.paging(conf.total, {
        format: "[< nncnn >]",
        perpage: conf.pageSize,
        page: conf.page,
        onClick: conf.onClick,
        onFormat: function(type) {
            switch (type) {
                case 'block': // n and c
                    if (!this.active) {
                        return "<a class='disable'>" + this.value + "</a>";
                    } else if (this.value == this.page) {
                        pagingObj.attr("current-page", this.value);
                        return "<a class='current'>" + this.value + "</a>";
                    } else {
                        return "<a>" + this.value + "</a>";
                    }

                    return '<a>' + this.value + '</a>';

                case 'next': // >

                    if (this.page == this.pages || this.pages == 0) {
                        return "<a  class='disable'>下一页</a>";
                    }
                    return '<a>下一页</a>';
                case 'prev': // <
                    if (this.page == 1) {
                        return "<a  class='disable'>上一页</a>";
                    }
                    return '<a>上一页</a>';
                case 'first': // [
                    if (this.page == 1) {
                        return "<a  class='disable'>首页</a>";
                    }
                    return '<a>首页</a>';
                case 'last': // ]
                    if (this.page == this.pages || this.pages == 0) {
                        return "<a  class='disable'>尾页</a>";
                    }
                    return '<a>尾页</a>';
            }
            return "";
        }
    });

    return paging;
};

commTemplUtil.initCommLeftMemu = function() {
    commTemplUtil.addTempl("leftmenuitems", USER_COMM_DATA.leftNavObj, "leftMenu");
};

commTemplUtil.initCommCategMenu = function(domId, data) {
    commTemplUtil.addTempl(domId, data, "goodsCateg");
    var mainObj = $("#" + domId);

    mainObj.find(".tab-head").find("span").first().addClass("active");
    mainObj.find("ul").first().css("display", "block");
}

/**
 * 设置头部导航栏 <br/>
 * @param currentItem: 当前第几个选项;
 */
commTemplUtil.setHeadCommNav = function(currentItem) {
    // todo,head-comm-nav
    $("#head-comm-nav").find("a").removeClass("current").eq(parseInt(currentItem || 0) - 1).addClass("current");
};

/**
 * 设置页面左侧 菜单栏 <br/>
 * @param currentItem: 当前第几个选项;
 */
commTemplUtil.setLeftMenu = function(currentItem) {
    // todo,head-comm-nav
    $("#leftmenuitems").find("a.link-txt").removeClass("current").eq(parseInt(currentItem || 0) - 1).addClass("current");
};

/**
 * 纯提示信息-比如: 错误消息 提示框.
 * @param conf: {title : "注意",message : "获取公司资料失败!",ok_txt : "确定"}
 * @author fsl-gongl.
 */
commTemplUtil.showErrorDialog = function (conf) {

    var def = {
        title : "注意",
        message : "请确认你的操作!",
        ok_txt : "确定"
    };

    var data = $.extend(def, conf);

    var tmplObj = _.template(commTemplUtil.type.errorDialog);
    var tmplObjHtml = tmplObj(data);

    var dia = dialog({
            id : "comm-error-box",
            title : data.title,
            content : tmplObjHtml,
            onshow : function () {
                var that = this;
                $("[id='content:comm-error-box']").find("#ok-btn").on("click", function () {
                    that.remove();
                });
            }
        });
    dia.showModal();

};

/**
 * 纯提示信息-比如: 错误消息 提示框.
 * @param conf: {title : "注意",message : "获取公司资料失败!", cancel_txt:"取消", ok_txt : "确定"}
 * @author fsl-gongl-20151107.
 */
commTemplUtil.showConfDialog = function (conf) {
    
    var def = {
        title : "",
        message : "请确认你的操作!",
        cancel_txt : "取消",
        ok_txt : "确定",
        ok_func : null,
        ok_context : null,
        ok_param : null
    };
    
    var data = $.extend(def, conf);
    var tmplObj = _.template(commTemplUtil.type.confirmDialog);
    var tmplObjHtml = tmplObj(data);
    
    var dia = dialog({
            id : "comm-comfirm-box",
            title : data.title,
            content : tmplObjHtml,
            onshow : function () {
                var that = this;
                // 绑定"取消"按钮
                $("[id='content:comm-comfirm-box']").find(".cancel-btn").on("click", function () {
                    that.remove();
                });
                
                 // 绑定"确定"按钮
                $("[id='content:comm-comfirm-box']").find(".ok-btn").on("click", function () {
                    if (!!data.ok_func) {
                        data.ok_func.call(data.ok_context, data.ok_param)
                    }
                    that.remove();
                });
            }
        });
    dia.showModal();
};

/**
 * 比较时间大小<br/>
 * @param date1 {date} 比如: 11/23/2015
 * @param date2 {date} 比如: 11/25/2015
 * @return {boolean}
 *          true: date1 小于(早于) date2;
 *          false: date1 大于(晚于) date2;
 *
 * @author fsl-gongl20151103
 */
commTemplUtil.checkDate = function(date1, date2) {

    var startDate = date1.replace(/-/g, "/");
    var endDate = date2.replace(/-/g, "/");

    var start = new Date(startDate);
    var end = new Date(endDate);

    if (start <= end) {
        return true;
    }
    return false;

};

$(function() {

    $.commNavTabs2 = function(headTitles, bodys, selClass) {
        headTitles.on("click", function() {

            $(this).addClass(selClass).siblings().removeClass(selClass);
            var idx = $(this).index();
            bodys.eq(idx).show().siblings().hide();

        });
    };

});